package com.keyancompany.zhongbaopingtai.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.adapter.MyLabelAdapter;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.LabelQuery;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.SPUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.OnClick;

/**
 * @author xihe
 *         项目标签
 */
public class TaskLabelActivity extends BaseActivity {

    @BindString(R.string.task_label)
    String projectTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.recycle)
    RecyclerView mRecyclerView;
    MyLabelAdapter adapter;

//    TaskLabelPopup labelPopup;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_tasklabel;
    }

    @Override
    protected int getMenuId() {
        return R.menu.label_add_menu;
    }

    @Override
    protected void initToolBar() {//toolbar.setTitle(projectTitle,);
        initToolBar(toolbar, projectTitle);
    }

    @Override
    protected void setUpViews() {
        initdata();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);// 创建一个线性布局管理器
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);// 默认是Vertical，可以不写
        mRecyclerView.setLayoutManager(layoutManager);// 设置布局管理器
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
//        adapter.setOnItemClickListener(new TeamManagerAdapter.OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, String i) {
//                if (labelPopup == null) {
//                    labelPopup = new TaskLabelPopup(TaskLabelActivity.this);
//                }
//                labelPopup.showAtLocation(view, Gravity.CENTER, 0, 0);
//            }
//        });



        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.label_create:
                        IntentUtils.startActivity(mContext, CreateLabelActivity.class);
                        break;
                }
                return false;
            }
        });

    }
    @Override
    protected void onResume() {
        super.onResume();
        initdata();
    }

    @OnClick(R.id.recycle)
    public void onClick() {
    }

    private void initdata() {
        if (isNetvaiable()) {
            String teamCode = SPUtils.getComment(mContext).getString(Constants.TEAM_CODE, null);
            TeamAPI.labelQuery(teamCode,TaskLabelActivity.this, getLoading(R.string.watting_hint));
        }
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);

        if (validateJson(json) != null) {
            BaseModel base = JSONObject.parseObject(json, BaseModel.class);
            LabelQuery query = JSONObject.parseObject(base.data, LabelQuery.class);

            if (base.result.equals(Constants.SUCCESS)) {
                if (query.getLabel().size() > 0) {
                    adapter = new MyLabelAdapter(query.getLabel());
                    mRecyclerView.setAdapter(adapter);
                }
                // 创建Adapter，并指定数据集
            } else {
                T.showShort(mContext, base.msg);
            }
        }
    }

    @Override
    public void failed(String error) {
        super.failed(error);
        T.showShort(mContext, error);
    }
}